package com.aube.interact.model;

import com.aube.rvi.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by huyaonan on 15/11/7.
 */
public class TimeLineItemDetail implements Serializable {

    /**
     * type = QA,VS,VOTE
     */
    public String answer="";
    public int revealTheAnswer=0;
    public List<PicItem> picList;
    public List<OptionItem> optionList;

    public static TimeLineItemDetail parse(JSONObject jsonObject) {
        if(jsonObject == null)
            return new TimeLineItemDetail();
        TimeLineItemDetail data = new TimeLineItemDetail();
        data.answer = jsonObject.optString("answer");
        data.revealTheAnswer = jsonObject.optInt("revealTheAnswer");
        data.cardnote = jsonObject.optString("cardnote");
        JSONArray picArray = jsonObject.optJSONArray("picList");
        if(picArray != null) {
            List<PicItem> picItems = new ArrayList<>();
            for(int i=0; i<picArray.length(); i++) {
                picItems.add(PicItem.parse(picArray.optJSONObject(i)));
            }
            data.picList = picItems;
        }
        JSONArray optionArray = jsonObject.optJSONArray("optionList");
        if(optionArray != null) {
            List<OptionItem> optionItems = new ArrayList<>();
            for(int i=0; i<optionArray.length(); i++) {
                optionItems.add(OptionItem.parse(optionArray.optJSONObject(i)));
            }
            data.optionList = optionItems;
        }
        return data;
    }

    public String getImgUrl() {
        return getImgUrl(PicItem.IMG_URL);
    }

    public String getImgTag() {
        return getImgUrl(PicItem.IMG_TAG);
    }

    private String getImgUrl(String type) {
        if(picList==null)
            return "";
        for(PicItem item : picList) {
            if(type.equalsIgnoreCase(item.type))
                return item.picurl;
        }
        return "";
    }

    /**
     * type = PIC
     */
    public PicItem pic;

    public void addToPicMap(HashMap<Integer, String> maps, int startTime) {
        if(pic != null) {
            //超过10s才提前，不然就算了
            if(startTime > 10) {
                startTime -= 10;
                maps.put(startTime, pic.picurl);
                startTime++;
            } else {
                return;
            }
        }

        if(picList==null)
            return;
        for(PicItem item : picList) {
            maps.put(startTime, item.picurl);
            startTime++;
        }
    }

    //新增卡片注释
    public String cardnote;

    //多镜头的倒计时时间
    public int timedown=0;

    public String getOptionId(int index) {
        if(optionList == null || optionList.size() <= index || index < 0)
            return null;
        return optionList.get(index).optionId;
    }

    public String getOptionValue(int index) {
        if(optionList == null || optionList.size() <= index)
            return "";
        return optionList.get(index).optionValue;
    }

    public int getOptionIndex(String optionId) {
        if(optionList == null || StringUtils.isBlank(optionId))
            return -1;
        int inex = 0;
        for(OptionItem item : optionList) {
            if(optionId.equalsIgnoreCase(item.optionId))
                return inex;
            inex++;
        }
        return -1;
    }

}
